package cn.itcast.order.web;


import cn.itcast.order.pojo.Order;

import cn.itcast.order.service.OrderService;

import com.itheima.feign.client.UserClient;
import com.itheima.feign.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;
//    @Autowired
//    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;
    @GetMapping("{orderId}")
    public Order queryOrderByUserId(@PathVariable("orderId") Long orderId) {
        // 根据id查询订单并返回
        Order order = orderService.queryOrderById(orderId);
        // 根据订单关联的用户id远程查询用户信息
//        Long userId = order.getUserId();                  // 服务名称
//        User user = restTemplate.getForObject("http://userservice/user/" + userId, User.class);
        User user = userClient.findById(order.getUserId());
        order.setUser(user);
        return order;
    }
}
